using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _2DGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Player : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Ship myShip;
        Vector2 cameraPosition;
        PlayerIndex index;
        Viewport viewport;
        HUD myHud;

        enum SpawnType { NotForced, Forced };

        public PlayerIndex Index
        {
            get { return index; }
        }

        public Ship MyShip
        {
            get { return myShip; }
        }

        float playerRespawnTimer;
        const float playerRespawnTime = 2.0f;
        const float clearSpawnRadius = 32.0f;

        public Player(Game game, PlayerIndex fromIndex, Viewport myViewport)
            : base(game)
        {
            index = fromIndex;
            viewport = myViewport;
            myHud = new HUD(this);
            // TODO: Construct any child components here

            Resources.World.AddPlayer(this);
        }


        bool SpawnNewShip(SpawnType check)
        {
            Ship newShip = new Ship("Ships//fighter_ship", this);

            Vector2 spawnPoint;
            
            if(Resources.World.Map.GetClearSpawnPoint(newShip.Texture.Height / 2, newShip.Texture.Width + clearSpawnRadius, out spawnPoint) == false)
            {
                if (check == SpawnType.Forced)
                {
                    spawnPoint = Resources.World.Map.GetRandomSpawnPoint(newShip.Texture.Height / 2);
                }
                else
                {
                    return false;
                }
            }

            myShip = newShip;
            Resources.World.AddObject(myShip);
            myShip.Teleport(spawnPoint);

            return true;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (myShip == null)
            {
                playerRespawnTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (playerRespawnTimer <= 0.0f)
                    SpawnNewShip(SpawnType.NotForced);
            }

            base.Update(gameTime);
            myHud.Update(gameTime);
        }

        public void ShipDestroyed()
        {
            myShip = null;
            playerRespawnTimer = playerRespawnTime;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if (myShip != null)
                cameraPosition = myShip.Position;

            Resources.World.Draw(viewport, cameraPosition);

            myHud.Draw(gameTime);
        }
   }
}